/* eslint-disable */
const { Controller } = require('egg');

class OutfitController extends Controller {
    // 查询搭配
    async index() {
        const { ctx } = this;
        const result = await ctx.service.outfit.findAll(ctx.query);
        ctx.success(result, '查询服装搭配信息成功');
    }

    // 创建搭配
    async create() {
        const { ctx } = this;
        const {user_id, title, description, category_id, image,is_personal } = ctx.request.body;
        const approval_status = (ctx.state.role === 'designer' || ctx.state.role === 'admin') ? 'approved' : 'pedding'; 
        const result = await ctx.service.outfit.add({user_id, title, description, category_id, image, is_personal,approval_status });
        ctx.success(result, '添加服装搭配信息成功');
    }

    // 删除搭配
    async destroy() {
        const { ctx } = this;
        const { id } = ctx.params;
        const success = await ctx.service.outfit.remove(id,ctx.state);
        if (success) {
            ctx.success(null, '数据删除成功');
        } else {
            ctx.error('数据删除失败');
        }

    }

    // 修改搭配
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        console.log('User in controller:', ctx.state.user);
        const success = await ctx.service.outfit.modify(ctx.request.body, id,ctx.state);
        if (success) {
            ctx.success(null, '数据更新成功');
        } else {
            ctx.error('数据更新失败');
        }
    }

    //审核搭配（搭配师操作）
    async approveOutfit() {
        const { ctx } = this;
        const id = ctx.params.id;
        const { approval_status } = ctx.request.body;

        const success = await ctx.service.outfit.approveOutfit(id, approval_status, ctx.state);
        if (success) {
            ctx.success(null, '审核通过');
        } else {
            ctx.error('审核未通过');
        }
    }
    // 查询单条数据
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.outfit.findById(id);
        ctx.success(result, `查询id为${id}的服装搭配信息成功`);
    }

}
module.exports = OutfitController